package com.pt.chitra.view.albumView.view
{
	import com.pt.chitra.config.ApplicationConfigs;
	import com.pt.chitra.config.ApplicationEventConstant;
	import com.pt.chitra.config.ApplicationNotificationConstant;
	import com.pt.chitra.view.albumView.controller.GetGalleriesCommand;
	import com.pt.chitra.view.albumView.controller.GetGalleriesImagesCommand;
	import com.pt.chitra.view.albumView.controller.OpenGalleryEvent;
	import com.pt.chitra.view.albumView.controller.ShowSelectedImagesEvent;
	
	import flash.events.Event;
	
	import mx.rpc.events.ResultEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class AlbumViewMediator extends Mediator implements IMediator
	{
		/*name of the Mediator */
        public static const NAME:String = "AlbumViewMediator";

		
		public function AlbumViewMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		//Do something -- Skulkarni
		/***
		 * Check for session object
		 * 
		 * */
		override public function onRegister():void
		{
			facade.registerCommand(ApplicationNotificationConstant.GET_GALLARY_INITAL_DATA,GetGalleriesCommand);
			sendNotification(ApplicationNotificationConstant.GET_GALLARY_INITAL_DATA);
			albumView.addEventListener(ApplicationEventConstant.OPEN_SELECTED_GALLERY,openGallery);
			albumView.addEventListener(ApplicationEventConstant.SHOW_SELECTED_IMAGE,showNextImage);
			albumView.addEventListener(ApplicationEventConstant.EDIT_SELECTED_IMAGE,editSelectedImage);
			albumView.addEventListener(ApplicationEventConstant.SHOW_MAIN_VIEW,showMainView);
		}
		
		
		// Do Something -- Skulkarni
		override public function onRemove():void
		{
		}
			
		override public function listNotificationInterests():Array
		{
			return [
				ApplicationNotificationConstant.GET_GALLARY_INITAL_DATA_RESULT,
				ApplicationNotificationConstant.GET_GALLARY_IMAGES_DATA_RESULT
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			 switch(notification.getName()){
				case ApplicationNotificationConstant.GET_GALLARY_INITAL_DATA_RESULT:
					albumView.getGalleriesHandler(notification.getBody()as ResultEvent);
				break;
				case ApplicationNotificationConstant.GET_GALLARY_IMAGES_DATA_RESULT:
					albumView.getImagesHandler(notification.getBody()as ResultEvent);
				break;
			}
		}
		
		private function openGallery(e:OpenGalleryEvent):void{
			var galleryID:int = (e._data).gallery_ID;
			albumView.selectedGallery = e._data;
			if(!facade.hasCommand(ApplicationNotificationConstant.GET_GALLARY_IMAGES_DATA)){
				facade.registerCommand(ApplicationNotificationConstant.GET_GALLARY_IMAGES_DATA,GetGalleriesImagesCommand);
			}
			sendNotification(ApplicationNotificationConstant.GET_GALLARY_IMAGES_DATA,galleryID);  
		}
		
		private function showNextImage(e:ShowSelectedImagesEvent):void{
			albumView.showImage(e._data,e._imagedata);
		}
		private function editSelectedImage(e:ShowSelectedImagesEvent):void{
			//file Path
			var imagePath:String = ApplicationConfigs.allowDomainURL+"/galleries/"+albumView.selectedGallery.name+"/"+e._data.filename;
			sendNotification(ApplicationNotificationConstant.CHANGE_TO_EDIT_VIEW);
			sendNotification(ApplicationNotificationConstant.EDIT_IMAGE_PATH,imagePath);
		}
		private function showMainView(e:Event):void{
			albumView.backToGalleries();
		}
		
		protected function get albumView():AlbumView
        {
            return viewComponent as AlbumView;
        }

		
	}
}